package com.cn.api.util; /**
 * @author: yihanyuan
 * @Description：
 * @Date: 2019/3/12 14:01
 */

import java.util.Arrays;

/**
 * @program: demo
 * @description:
 * @author: yihanyuan
 * @create: 2019-03-12 14:01
 **/
public class Test5 {

    public static void each(Callback callback, int times) {
//        int[] indexes = new int[times];
//        Arrays.fill(indexes, -1);
        //价格列表
        int[] indexes = {25, 30, 20};
        //满减价格
        int fullReduction = 90;
        each0(callback, times, 0, indexes);
    }

    /**
     * @return
     * @description 真正执行多层嵌套循环的地方
     * @author YiHanYuan
     * @params
     * @date 2019/3/13 13:31
     **/
    private static void each0(Callback callback, int times, int nextTimes,
                              int[] indexes) {
        //如果当前循环的次数超过的话跳出这一次循环，进行下一次
        if (nextTimes >= times)
            return;
        for (int i = 0; i < times; i++) {
            indexes[nextTimes] = i;
            int[] ints = new int[indexes.length];
//            for (int k = indexes.length; --k > nextTimes; ) {
//                indexes[k] = -1;
//            }
            //如果这次的嵌套循环次数没达到，不执行以下代码
            if (nextTimes == indexes.length - 1)
                callback.call(indexes, nextTimes);
            //迭代 ，对times更改，因为每次循环的次数可能不一致
            each0(callback, times, nextTimes + 1, indexes);
        }
    }

    public static interface Callback {
        public void call(int[] indexes, int deep);
    }

    public static void main(String[] args) {

        Test5.each(new Callback() {
            int i = 1;

            public void call(int[] indexes, int deep) {
                System.out.println(Arrays.toString(indexes) + ", 当前嵌套层数:" + deep + "执行了" + (i++) + "次");
            }
        }, 3);
    }
//给分吧，呵呵.deep表示当前嵌套层数.times表示循环特定的次数。callback是业务代码.
}
